package com.oim.ui.fx.classics.setting;

import com.oim.core.business.constant.Setting;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.ColorPicker;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;

import java.util.function.BiConsumer;


/**
 * @author XiaHui
 * @date 2017-12-08 20:34:10
 */
public class FontSettingPane extends VBox {

    ColorPicker colorPicker;
    Text text = new Text("测试Abc");
    ChoiceBox<Integer> sizeList;

    public FontSettingPane() {
        initComponent();
        initEvent();
    }

    private void initComponent() {
        Label label = new Label("字体:");
        updateText();
        sizeList = new ChoiceBox<>();
        sizeList.getItems().addAll(12, 16, 20, 24, 30, 38, 45, 50);
        sizeList.getSelectionModel().select(Setting.fontSize);
        colorPicker = new ColorPicker();
        colorPicker.setValue(Color.web(Setting.color));
        getChildren().addAll(label, sizeList, colorPicker, text);
    }

    private void initEvent() {
        sizeList.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) ->
        {
            Setting.fontSize = newValue;
            updateText();
        });
    }

    public void setConsumer(BiConsumer<String, Integer> consumer) {
        colorPicker.setOnAction(actionEvent ->
                {
                    String value = colorPicker.getValue().toString().replace("0x", "");
                    Setting.color = value;
                    updateText();
                    consumer.accept(value, Setting.fontSize);
                }
        );
    }

    void updateText() {
        text.setFill(Color.web(Setting.color));
        text.setFont(Font.font(Setting.fontSize));
    }
}
